//
//  AccountView.m
//  01_拉勾
//
//  Created by dllo on 16/7/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AccountView.h"

@implementation AccountView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame image:(UIImage *)image {
    if (self = [super initWithFrame:frame]) {
        [self createView:image backgroundColor:[UIColor colorWithRed:24/255.0 green:17/255.0 blue:16/255.0 alpha:1]];
    }
    return self;
}


- (instancetype)initWithFrame:(CGRect)frame image:(UIImage *)image backgroundColor:(UIColor *)color {
    if (self = [super initWithFrame:frame]) {
        [self createView:image backgroundColor:color];
    }
    return self;
}

- (void)createView:(UIImage *)image backgroundColor:(UIColor *)color{
    self.image = image;
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.height, self.frame.size.height)];
    backView.backgroundColor = color;
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(16, 15, 23, 26)];
    imageView.image = self.image;
    
    [backView addSubview:imageView];
    
    [self addSubview:backView];
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(self.frame.size.height, 0, self.frame.size.width - self.frame.size.height, self.frame.size.height)];
    _textField.backgroundColor = color;
    _textField.textColor = [UIColor lightGrayColor];
    [self addSubview:_textField];
    _textField.delegate = self;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField{
    CGFloat h = textField.center.y - self.superview.frame.size.height / 2;
    if (h > 0) {
        self.superview.center = CGPointMake(self.superview.center.x, self.superview.center.y - h);
    }
    return YES;
}

- (BOOL) textFieldShouldEndEditing:(UITextField *)textField{
    //    self.view.frame = CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height);
    CGFloat h = textField.center.y - self.superview.frame.size.height / 2;
    if (h > 0) {
        self.superview.center = CGPointMake(self.superview.center.x, self.superview.center.y + h);
    }
    return YES;
}

@end
